ajaxurl   	= '' ;                  //აუცილებლათ შესავსები ველი
tname    	= 'example' ;           //გლობალური თეიბლის იდ
fname 		= 'add_edit_form' ;     //გლობალური დიალოგის ფორმის იდ
tbname   	= 'tabs' ;              //გლობალური ტაბის იდ
param     	= new Object() ;        //პარამეტრების შესანახათ
total    	= [] ;                  //ჯამებისათვის
sort     	= ['0', 'asc'] ;        //სორტირებისათვის
activetab 	= '0' ;                 //აქტიური ტაბის ნომერი
procesing 	= 'client' ;            //გლობალური ტაბლეს პროცესინგის ტიპი
addbutton   =  'add_button';        //
deletebutton=  'delete_button';     //
temp_table = {};
length 	 	= [[15, 30, 50, -1], [15, 30, 50, "ყველა"]]; //
imex   		= {};
imex.aButtons=new Array();
imex.aButtons[1]={};
imex.sSwfPath="media/swf/copy_csv_xls.swf"
imex.aButtons[0]='copy';
imex.aButtons[1].sExtends = "xls";
imex.aButtons[1].sFileName = GetDateTime(1) + ".csv";
imex.aButtons[2]='print';

//*--------------------------------------------დათათეიბლი---------------------------------------
function datatable(){
	this.tname 			=  tname;                 //დათატეიბლის თეიბლის იდ
	this.ajaxurl		=  ajaxurl;
	this.fname 			=  fname;
	this.tbname 		=  tbname;
	this.param 			=  param;	
	this.total 			=  [];
	this.param.act 		=  "get_list";
	this.imex 			=  imex
	this.sort 			=  sort;	
	this.length 		=  length;
	this.selectet       =  true;
	this.dialog 	    =  new Object();
	this.table 			=  new Object();
	this.table.processing	= true;
	this.table.serverSide	= true;
	this.table.bDestroy 	= true;
	this.table.bJQueryUI	= true;
	this.table.sDom			= "<'dataTable_buttons'T><'H'lfrt><'dataTable_content't><'F'ip>";
	this.table.oTableTools	= this.imex;
	this.table.sPaginationType = "full_numbers";
	this.table.aaSorting	=this.sort;
	this.table.iDisplayLength = this.length[0][0];
	this.table.aLengthMenu	=this.length;
	this.table.multipleSelection= true;	
	this.fnFooterCallback = function(nRow, aaData, iStart, iEnd, aiDisplay){		
		try {	
			var  total = this[0].data.total;
			if(this[0].data.table.serverSide){		
				for (var i = 0; i < total.length; i++) {
		    		var sum=0;
		    		var sum1=0;
		    		var  t=total[i][0];
		    		var  l=total[i][1]; 
		        	for (var j = 0; j < aiDisplay.length; j++) {
		        		if(total[i][2]== "time"){	
		        			seconds=time2sec(aaData[j][t])		        				        			
		        			sum1+=parseFloat(seconds)	
		        			sum=sec2time(sum1)
		        		}else{		        		
		        		sum1+=parseFloat(aaData[j][t]);
		        		sum=sum1.toFixed(2);
		        			}
		        		}  	
		    			$($(nRow).children()[t]).html(sum+" "+l );
		    				}
			}else{				
				var  total = this[0].data.total;
					for (var i = 0; i < total.length; i++) {
			    		var sum=0;
			    		var fsum=0;
			    		var sum1=0;
			    		var fsum1=0;
			    		var  t=total[i][0];
			    		var  l=total[i][1];
			        	for (var k = iStart; k < iEnd; k++) {
			        		if(total[i][2]== "time"){	
			        			seconds=time2sec(aaData[k][t])		        				        			
			        			sum1+=parseFloat(seconds)	
			        			sum=sec2time(sum1)
			        		}else{
			        			sum1+=parseFloat(aaData[k][t])
			        			sum=sum1.toFixed(2);
			        			}        		
			        	}
			        	for (var j = 0; j < aiDisplay.length; j++) {
			        		if(total[i][2]== "time"){	
			        			seconds=time2sec(aaData[j][t])		        				        			
			        			fsum1+=parseFloat(seconds)	
			        			fsum=sec2time(fsum1)
			        		}else{			        			
			        			fsum1+=parseFloat(aaData[j][t])
			        			fsum=fsum1.toFixed(2);
			        			}
			        		}
			    			$($(nRow).children()[t]).html(sum+""+l+" <br /> "+ fsum );
			    				}	
	
				}	
		}catch(err) {}
		};
		
	//}
	this.table.fnFooterCallback  =this.fnFooterCallback;	
	this.table.oLanguage	={};
	this.table.oLanguage.sProcessing	="იტვირთება...";
	this.table.oLanguage.sLengthMenu	="ნახე _MENU_ ჩანაწერი",
	this.table.oLanguage.sZeroRecords	="ჩანაწერი ვერ მოიძებნა",
	this.table.oLanguage.sInfo			="_START_-დან _END_-მდე სულ: _TOTAL_",
	this.table.oLanguage.sInfoEmpty		="0-დან 0-მდე სულ: 0";
	this.table.oLanguage.sInfoFiltered	="(გაიფილტრა _MAX_-დან _TOTAL_ ჩანაწერი)",
	this.table.oLanguage.sSearch		="ძიება",
	this.table.oLanguage.oPaginate		={};
	this.table.oLanguage.oPaginate.sFirst="პირველი",
	this.table.oLanguage.oPaginate.sPrevious	= 	"წინა",
	this.table.oLanguage.oPaginate.sNext		=	"შემდეგი",
	this.table.oLanguage.oPaginate.sLast		=	"ბოლო";
	this.table.ajax						=	{}
	this.table.ajax.url 				= 	this.ajaxurl;
	this.table.ajax.type				=	"POST";
	//this.table.ajax.async 				=   false;
	this.table.ajax.data			    =	this.param;
	
	
	
	

	this.get 	        =  function (){
		
		
		
		
		
		
		
		this.datatable = $("#" + this.tname).dataTable(this.table);
		this.datatable[0].data=this;		
		this.datatable.search=$("#" + this.tname + " thead input");
		this.datatable.search.keyup(this,function (event) {
			this.parent=event.data;
			this.parent.datatable.fnFilter(this.value, this.parent.datatable.search.index(this));
    });
    asInitVals={};
    
    

    
    this.datatable.search.each(function (i) {
        asInitVals[i] = this.value;
    });

    this.datatable.search.focus(function () {
        if (this.className == "search_init") {
            this.className = "";
            this.value = "";
        }
    });

    this.datatable.search.blur(function (i) {
        if (this.value == "") {
            this.className = "search_init";
            this.value = asInitVals[datatable.search.index(this)];
        }
    });

    $(".DTTT_button").hover(
		  function () {
		    $(this).addClass("ui-state-hover");
		  },
		  function () {
		    $(this).removeClass("ui-state-hover");
		  }
    );
    this.datatable.dialog=this.dialog;
    $("#" + this.tname + " tbody").off("click");

    if (this.selectet){
    $("#" + this.tname + " tbody").on("click","tr",this, function (event) {
    	 $(this).toggleClass('selected');
    })}
    $("#" + this.tname + " tbody").off("dblclick");
    $("#" + this.tname + " tbody").on("dblclick","tr",this, function (event) {
    	 this.parent=event.data;
         nTds = $("td", this);
         rID = $(nTds[0]).text();
         if(this.parent.datatable.dialog.param!=undefined){
		         this.parent.datatable.dialog.param.id=rID;
		         this.parent.datatable.dialog.open();
         };
    });
    activetable=this;
};


};
//------------------------------დიალოგი-----------------------------------
function dialog(){
	this.fname = fname;
	this.param = new Object() ;
	this.param.act="dialog";
	this.param.tab={};
	this.param.fname=fname;
	this.param.id='';
	this.param.data = new Object();
	this.button = new Object();
	this.button.save = new Object();
	this.button.save.text  = "შენახვა";
	this.button.save.id    = "save-dialog";
	this.button.save.click = function (){};
	this.button.cancel = new Object();
	this.button.cancel.text="დახურვა";
	this.button.cancel.id="cancel-dialog";
	this.button.cancel.click=function () {$(this).dialog("close"); };
	this.arr ={};

	this.ajax= new Object();
	this.ajax.dataType= "json";
	this.ajax.url= ajaxurl,
	this.ajax.async= false,
	this.ajax.data= this.param,
	this.ajax.success = function(data,event) {
		this.parent=bufer;
		bufer='';
		$("#"+this.parent.fname).html(data.page)
			  };
	this.open = new Object();
	this.open.param = this.param;

	this.save  = function () {
//-------------------მონიშნულების ამოღება-------------------------------------------------------------------------
		this.arr.html=$("#" + this.fname + " input, #" + this.fname + " select, #" + this.fname + " textarea");  	// 	ყველა მონიშნული
		for (var i = 0; i < this.arr.html.length; i++){																//
			this.param.data[($(this.arr.html[i]).attr('id'))]=$(this.arr.html[i]).val();}							//	პირველი ელემენტის ამოღება
//-------------------------------------------------------------------------------------------------------------------
		bufer=this;
		$.ajax(this.ajax);
		$("#"+this.fname).dialog("close");
	}

	this.open = function(){
		$("#" + this.fname).dialog({
	        	position: "top",
	            resizable: false,
	            width: 'auto',
	            height: 'auto',
	            modal: true,
	            stack: false,
	            dialogClass: this.fname + "-class",
	            buttons: this.button
        });
		console.log(this);
	bufer=this;
	$.ajax(this.ajax);
	$("#"+this.button.save.id).off("click");
	$("#"+this.button.save.id).on( "click", this, function (event){
		this.parent=event.data;
		this.parent.save();//
		});

	}


}
//**--------------------------------------------------ბუტონს-------------------------------------------------------------
function getburttons(){
	this.addbutton    =  addbutton;
	this.deletebutton =  deletebutton;
	this.name         =  '';
	this.datatable    =  {};
	this.dialog       =  {};
	this.param 		  =  {};
	this.del          = function () {
    	this.param.act='delete';
    	this.arr = new Object();
    	this.param.list= new Object();
    	this.arr = $("#" + this.datatable.tname + " tbody .selected");

    	for (var i = 0; i < this.arr.length; i++) {
    		 this.tr= $("td",this.arr[i]);
    		 this.param.list[i]=($(this.tr[0]).text());}

    	console.log(this.param.list);

	}
	this.add          = function () {
    	this.dialog.param.id='0';
    	this.dialog.param.act='add';
		this.dialog.open();
	}

	$('#'+ this.addbutton).button({
        icons: {primary: "ui-icon-plus"}
    }).click(this, function(event) {
    	this.parent=event.data;
    	this.parent.add();
    });
	$('#'+ this.deletebutton).button({
        icons: {primary: "ui-icon-trash"}
    }).click(this, function(event) {
    	this.parent=event.data;
    	this.parent.del();
    		});
}
//**-------------------------------------------------- პიკერ -------------------------------------------------------
function picker(){
	this.param={};
	this.time=false;
	this.date=new Date();
	this.format="yy-mm-dd";
	this.param.dateFormat = this.format;
	this.get= function (){
		if(this.time){
			this.picker=$("#" + this.name).datetimepicker(this.param);
		}else{
			this.picker=$("#" + this.name).datepicker(this.param);
		}
	this.picker.datepicker("setDate", this.date);

	}
}

//**-------------------------------------------------- ტაბები ------------------------------------------------------
function tabs(){
	this.tbname=tbname;
	this.tname=tname;
	this.tabid=$("#" + this.tbname);
	this.tabid.data="sddsd";
	this.get=function(){
		this.tabs=this.tabid.tabs();
		this.tabid.on( "tabsactivate", this ,function( event, ui ) {
			this.parent=event.data;
			this.active = this.parent.tabid.tabs("option", "active");
			this.tname=this.parent.tname + this.active;
			this.datatable=eval("("+this.tname+")");
			this.datatable.get();
			//window.location.hash = ui.newPanel.selector;
		} );
	}

}


function sec2time(sec){
    var sec_num = parseInt(sec, 10);
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);
    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    var time    = hours+':'+minutes+':'+seconds;
    return time;
    }
function time2sec(time){	
	var a = time.split(':'); 		        			
	var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
return seconds;
}
